/**
* 左边菜单
*/ 
<template>
  <el-menu
    default-active="2"
    :collapse="collapsed"
    collapse-transition
    router
    :default-active="$route.path"
    unique-opened
    class="el-menu-vertical-demo"
    background-color="#334157"
    text-color="#fff"
    active-text-color="#ffd04b"
  >
    <div class="logobox">
      <img
        class="logoimg"
        src="../assets/img/logo.png"
        alt=""
        @click="toFirst"
      />
    </div>
    <el-submenu v-for="menu in allmenu" :key="menu.id" :index="menu.name">
      <template slot="title">
        <i class="iconfont" :class="menu.icon"></i>
        <span>{{ menu.name }}</span>
      </template>
      <el-menu-item-group>
        <el-menu-item
          v-for="chmenu in menu.subMenus"
          :index="'/' + chmenu.url"
          :key="chmenu.id"
        >
          <i class="iconfont" :class="chmenu.icon"></i>
          <span>{{ chmenu.name }}</span>
        </el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>
<script>
export default {
  name: "leftnav",
  data() {
    return {
      collapsed: false,
      fmenus: [],
      allmenu: [],
    };
  },
  // 创建完毕状态(里面是操作)
  created() {
    this.getParentMenus();
  },
  methods: {
    toFirst() {
      this.$router.push("/dashbord");
    },
    async getParentMenus() {
      var d = await this.$axios.get("/menu/parentMenu");
      if (d.code == 200) {
        this.fmenus = d.data;
        this.initMenus();
      }
    },
    initMenus() {
      let f = this.fmenus;
      var d = JSON.parse(window.sessionStorage.getItem("menus"));

      //子菜单
      for (let i = 0; i < f.length; i++) {
        let subMenus = [];
        for (let j = 0; j < d.length; j++) {
          if (f[i].id == d[j].parentId) {
            subMenus.push(d[j]);
          }
        }
        f[i].subMenus = subMenus;
      }

      this.allmenu = f;
    },
  },
};
</script>
<style>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 240px;
  min-height: 400px;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  border: none;
  text-align: left;
}
.el-menu-item-group__title {
  padding: 0px;
}
.el-menu-bg {
  background-color: #1f2d3d !important;
}
.el-menu {
  border: none;
}
.logobox {
  height: 40px;
  line-height: 40px;
  color: #9d9d9d;
  font-size: 20px;
  text-align: center;
  padding: 20px 0px;
}
.logoimg {
  height: 40px;
}
</style>